const avideoInfo = {};
let avideoContext = uni.createInnerAudioContext();
let prevAvideoLink = '';
let isPlayPause = false
let isPlayFlag = false;

avideoInfo.setPlayAVideo = function(avideoLink, looping = '') {
	isPlayFlag = false;
	if (looping === 'looping') isPlayFlag = true;

	if (isPlayPause) {
		if (prevAvideoLink == avideoLink) {
			avideoContext.play()
		} else {
			avideoContext.stop()
			avideoContext.src = avideoLink
			prevAvideoLink = avideoLink
			avideoContext.onCanplay(() => {
				avideoContext.play()
			})
		}
	} else { 
		if (prevAvideoLink == avideoLink) {
			avideoContext.pause()
			isPlayPause = true
		} else {
			avideoContext.stop()
			avideoContext.src = avideoLink
			prevAvideoLink = avideoLink
			avideoContext.onCanplay(() => {
				avideoContext.play()
			})
		}
	}
}
avideoInfo.setStopAVideo = function() {
	avideoContext.stop()
}
avideoInfo.setPauseAVideo = function() {
	avideoContext.pause()
}

avideoContext.onPlay(() => {
	isPlayPause = false
});
avideoContext.onPause(() => {
	isPlayPause = true
});
avideoContext.onEnded(() => {
	isPlayPause = true
	if (isPlayFlag) {
		avideoInfo.setPlayAVideo(avideoContext.src, 'looping')
	}
});

export default avideoInfo;